﻿// See https://aka.ms/new-console-template for more information
// 发布事件
using eventBusDemo;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddSingleton<IEventBus, InMemoryEventBus>();
services.AddTransient<OrderCreatedEventHandler>();
services.AddTransient<OrderCreatedEvent>();

var serviceProvider = services.BuildServiceProvider();

// 2. 获取事件总线实例
var eventBus = serviceProvider.GetService<IEventBus>();

// 3. 订阅事件
eventBus.Subscribe<OrderCreatedEvent, OrderCreatedEventHandler>();

// 4. 发布事件
var orderCreatedEvent = new OrderCreatedEvent("12345");
Console.WriteLine("Publishing OrderCreatedEvent...");
eventBus.Publish(orderCreatedEvent);

Console.WriteLine("Event published and handled.");
Console.ReadLine();
